﻿$.extend({

    Editor: {

        fileManagerUrl: '/flexexplorer/',
        modes: {

            small: {
                plugins: 'autolink,save,directionality,tabfocus,emotions,inlinepopups,paste,visualchars,xhtmlxtras,fullscreen,syntaxhl',
                theme_advanced_buttons1: 'bold,italic,underline,strikethrough,|,fontselect,fontsizeselect,undo,redo,|,justifyleft,justifycenter,justifyright,justifyfull,|,link,unlink,|,syntaxhl,image,fullscreen',
                theme_advanced_buttons2: '',
                theme_advanced_buttons3: '',
                theme_advanced_buttons4: '',
                inlinepopups_skin: 'office2007blue'

            },
            medium: {

                plugins: 'autolink,save,tabfocus,table,advimage,iespell,advlink,emotions,inlinepopups,media,directionality,visualchars,noneditable,pagebreak,nonbreaking,xhtmlxtras,preview,fullscreen,syntaxhl',
                theme_advanced_buttons1: "link,unlink,anchor,|,search,replace,|,bullist,numlist,outdent,indent,blockquote,|,sub,sup,|,ltr,rtl,|,cite,abbr,acronym,del,ins,attribs,|,charmap,iespell,advhr,pagebreak,|,preview,fullscreen",
                theme_advanced_buttons2: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,forecolor,styleprops,backcolor,fontselect,fontsizeselect,formatselect,|,emotions,image,syntaxhl",
                theme_advanced_buttons3: '',
                theme_advanced_buttons4: '',
                inlinepopups_skin: 'office2007blue'

                //file_browser_callback: '$.Editor.fileCallback'
            },

            full: {
                plugins: 'pdw,autolink,save,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template',
                theme_advanced_buttons1: 'pdw_toggle,paste,pastetext,pasteword,|,undo,redo,cleanup,|,tablecontrols,|,hr,removeformat,visualaid,|,insertlayer,moveforward,movebackward,absolute',
                theme_advanced_buttons2: 'search,replace,|,bullist,numlist,outdent,indent,blockquote,|,sub,sup,|,ltr,rtl,|,cite,abbr,acronym,del,ins,attribs,|,charmap,emotions,iespell,advhr,|,image,media,|,code,preview,fullscreen',
                theme_advanced_buttons3: 'styleprops,forecolor,backcolor,fontselect,fontsizeselect,formatselect,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,link,unlink,anchor',
                theme_advanced_buttons4: '',
                pdw_toggle_on: 1,
                pdw_toggle_toolbars: "2",
                inlinepopups_skin: 'office2007blue'
                //file_browser_callback: '$.Editor.fileCallback'
            }

        },

        defaults: {

            meta: 'tinymce',
            mode: 'small',
           /* script_url: '/tiny_mce/tiny_mce.js',*/
           script_url: '/tiny_mce/tiny_mce_gzip.axd',
            skin: "cirkuit",
            theme: "advanced",
            theme_advanced_toolbar_location: "top",
            theme_advanced_toolbar_align: "left",
            theme_advanced_statusbar_location: "bottom",
            theme_advanced_resizing: true,
            paste_auto_cleanup_on_paste: true,
            remove_linebreaks: false,
            forced_root_block: false,
            force_br_newlines: true,
            force_p_newlines: false,
            theme_advanced_resize_horizontal: false,
            extended_valid_elements: "pre[name|class]",  
            convert_urls: false,

            entity_encoding: "raw",
            verify_html: false,
            onchange_callback: function(ed) {
                ed.triggerSave();
            },
            template_replace_values: {
                username: "Nasa8x",
                staffid: "118"
            }

        },
        showFileManager: function () {

            var f = {
                url: $.Editor.fileManagerUrl + '?tinymce=1&returnFull=1',
                width: screen.width * 0.8, // pixel
                height: screen.height * 0.8
            };

            var a = {
                inline: false, // only works if inlinepopups plugin is available
                window: window
            };

            // Use TinyMCE window API
            if (window.tinymce && tinyMCE.activeEditor) {
                a.editor = tinyMCE.activeEditor;
                return tinyMCE.activeEditor.windowManager.open(f, a);
            }


            // Use jQuery WindowManager
            if (window.jQuery && jQuery.WindowManager)
                return jQuery.WindowManager.open(f, a);

            return false;
        },

        fileCallback: function (field_name, url, type, win) {

            if (type == "media") {
                type = win.document.getElementById("media_type").value;
            }

            switch (type) {
                case "image":
                    type = "images";
                    break;
                case "media":
                case "qt":
                case "wmp":
                case "rmp":
                    type = "media";
                    break;
                case "shockwave":
                case "flash":
                    type = "flash";
                    break;

                case "file":
                default:
                    type = "files";
                    break;

            }

            var f = {
                url: $.Editor.fileManagerUrl + '?tinymce=1&returnFull=1&type=' + type,
                width: screen.width * 0.8, // pixel
                height: screen.height * 0.8,
                resizable: "yes",
                center: 1,
                inline: 1,  // This parameter only has an effect if you use the inlinepopups plugin!
                close_previous: "no"
            };

            var a = {
                inline: false, // only works if inlinepopups plugin is available
                window: win,
                input: field_name
            };

            // Use TinyMCE window API
            if (window.tinymce && tinyMCE.activeEditor) {
                a.editor = tinyMCE.activeEditor;
                return tinyMCE.activeEditor.windowManager.open(f, a);
            }


            // Use jQuery WindowManager
            if (window.jQuery && jQuery.WindowManager)
                return jQuery.WindowManager.open(f, a);

            return false;
        },

        init: function (o) {

            /* if (!$.fn.tinymce) {
            setTimeout(function () {
            $.Editor.init({ meta: 'tinymce' });
            }, 500);
            return;
            };*/

            $('textarea.tinymce').each(function () {

                var e = $(this);

                if ($.data(e, 'tinymce'))
                    return;

                $.data(e, 'tinymce', true);

                var s = $.extend({}, $.Editor.defaults, o || {});

                if (s.meta && $.metadata) {
                    var md = e.metadata();

                    var _o = md[s.meta];

                    if (_o && _o.file_browser_callback) {

                        _o.file_browser_callback = '$.Editor.fileCallback';
                    }

                    $.extend(true, s, _o || {});
                }

                if (s.mode == 'small') {
                    $.extend(s, $.Editor.modes.small);
                }
                else if (s.mode == 'medium') {

                    $.extend(s, $.Editor.modes.medium);
                }
                else {
                    $.extend(s, $.Editor.modes.full);
                }


                
                e.tinymce(s);

            });

        }
    }


});

$(function () {
    try {

        $.Editor.init();
        
    } catch(e) {

    } 
    
});
    

